package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ConfirmShipServlet extends HttpServlet {
    private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/confirmOrder.jsp";

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String shipToFirstName = req.getParameter("shipToFirstName");
        String shipToLastName = req.getParameter("shipToLastName");
        String shipAddress1 = req.getParameter("shipAddress1");
        String shipAddress2 = req.getParameter("shipAddress2");
        String shipCity = req.getParameter("shipCity");
        String shipState = req.getParameter("shipState");
        String shipZip = req.getParameter("shipZip");
        String shipCountry = req.getParameter("shipCountry");

        HttpSession session = req.getSession();
        Order order = (Order)session.getAttribute("order");

        order.setShipToFirstName(shipToFirstName);
        order.setShipToLastName(shipToLastName);
        order.setShipAddress1(shipAddress1);
        order.setShipAddress2(shipAddress2);
        order.setShipCity(shipCity);
        order.setShipState(shipState);
        order.setShipZip(shipZip);
        order.setShipCountry(shipCountry);
        //覆盖原来的order
        session.setAttribute("order",order);



        Account account = (Account) session.getAttribute("account");
        if(account != null) {
            String strUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();
            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") +"用户" +account.getUsername()+  "  "+"跳转至订单确认页面";
            logService.insertLogInfo(account.getUsername(),logInfo);
        }

        req.getRequestDispatcher(CONFIRM_ORDER).forward(req,resp);
    }
}
